package com.dmz.wxcp.wecom.wxbusiness.service.impl;

import com.dmz.wxcp.constants.Constant;
import com.dmz.wxcp.redis.RedisUtils;
import com.dmz.wxcp.utils.PageUtil;
import com.dmz.wxcp.vo.req.ProductReqVO;
import com.dmz.wxcp.vo.resp.PageRespVO;
import com.dmz.wxcp.wecom.common.service.RedisService;
import com.dmz.wxcp.wecom.wxbusiness.mapper.PurchasingPriceMapper;
import com.dmz.wxcp.wecom.wxbusiness.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class IndexServiceImpl implements IndexService {


    @Autowired
    private PurchasingPriceMapper purchasingPriceMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public PageRespVO<Object> getPriceObject(ProductReqVO vo) {
        String mobilepricelist= Constant.MOBILE_PRICELIST+"_"+vo.getPageSize().toString()
                +"_"+vo.getCurrentPage().toString()+"_"+vo.getCategoryName()+"_"+vo.getProductName()+"_"+vo.getProductNum();

        List<Object> result= new ArrayList<>();

        if(redisService.hasKey(mobilepricelist)){
            result= RedisUtils.RedisToObjectList(redisService.get(mobilepricelist).toString(),Object.class);
            return PageUtil.getPageVo(result);
        }else{
            result=purchasingPriceMapper.getmobilepriceListsByProcedure(vo);
            redisService.set(mobilepricelist,result,5, TimeUnit.MINUTES);

            return PageUtil.getPageVo(result,true);
        }

    }



}
